//
//  XMAccountSafeViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMAccountSafeViewController.h"
#import "XMMineSettingLine.h"
#import "XMRechargeCountViewController.h"
#import "XMAddressListViewController.h"
#import "XMZhuXiaoTipView.h"
#import "XMBindPhoneLoginViewController.h"
#import "XMNoPhoneLoginViewController.h"
#import "XMWXApiManager.h"

@interface XMAccountSafeViewController () <XMWXApiManagerDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (nonatomic, strong) XMMineSettingLine *phoneV;
@property (nonatomic, strong) XMMineSettingLine *addressV;
//@property (nonatomic, strong) XMMineSettingLine *rechargeV;
@property (nonatomic, strong) XMZhuXiaoTipView *zhuxiaoTipV;
@property (nonatomic, strong) XMMineSettingLine *bindWXTipV;

@end

@implementation XMAccountSafeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
    
    [kWXApiManager registerAppKey];
    kWXApiManager.delegate = self;
}

#pragma mark - setupSubviews

- (void)setupSubviews {
    self.navView.backgroundColor = kHEX(@"#F2F2F2");
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    
    [self.navLeft2Btn setTitle:@"账号与安全" forState:UIControlStateNormal];
    
    self.phoneV = [XMMineSettingLine viewFromXib];
    self.phoneV.titleLa.text = @"手机绑定";
    self.phoneV.descLa.text = kUserModel.mobile;
    self.phoneV.descLa.textColor = kAppMainColor;
    
    self.addressV = [XMMineSettingLine viewFromXib];
    self.addressV.titleLa.text = @"地址管理";
    
    
    
    self.bindWXTipV = [XMMineSettingLine viewFromXib];
    self.bindWXTipV.titleLa.text = @"绑定微信";
    self.bindWXTipV.descLa.text = kUserModel.bind_wechat_status ? @"已绑定" : @"未绑定";
    
//    self.bindWXTipV.descLa.textColor = kAppMainColor;
//    self.rechargeV = [XMMineSettingLine viewFromXib];
//    self.rechargeV.titleLa.text = @"充值设置";
//    self.rechargeV.descLa.text = @"每日限额";
    
    [self.bgView addSubview:self.phoneV];
    [self.bgView addSubview:self.addressV];
    [self.bgView addSubview:self.bindWXTipV];

    [_phoneV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(_bgView);
        make.height.mas_equalTo(50);
    }];
    
    [_addressV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_bgView);
        make.height.mas_equalTo(50);
        make.top.equalTo(_phoneV.mas_bottom);
    }];
    
    [_bindWXTipV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_bgView);
        make.height.mas_equalTo(50);
        make.top.equalTo(_addressV.mas_bottom);
    }];
    
    XMWeakSelf(self);
//    _rechargeV.clickBlock = ^{
//        XMStrongSelf(self);
//        XMRechargeCountViewController *vc = [[XMRechargeCountViewController alloc] initWithNibName:kStringFromClass(XMRechargeCountViewController) bundle:nil];
//        [self.navigationController pushViewController:vc animated:YES];
//    };

    _bindWXTipV.clickBlock = ^{
        if (kUserModel.bind_wechat_status) {
            return;
        }
        [kWXApiManager weChatLogin];
        
    };
    
    _addressV.clickBlock = ^{
        XMStrongSelf(self);
        XMAddressListViewController *vc = [[XMAddressListViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    self.zhuxiaoTipV.cancelBlock = ^{
        [GKCover hideCover];
    };
    
    self.zhuxiaoTipV.sureBlock = ^{
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        NSMutableDictionary *dic = @{}.mutableCopy;
        [YHNetworkManager.sharedInstance postWithUrl:@"api/user/delete" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [GKCover hideCover];
                [MBProgressHUD hideHUD];
                [MBProgressHUD showHUDMsg:@"注销成功" toView:nil];
                [kNotificationCenter postNotificationName:@"xm_logout" object:nil];
            }
        }];
    };
    
    _phoneV.clickBlock = ^{
        XMStrongSelf(self);
        if ([XMUtil isBlankString:kUserModel.mobile]) {
            XMNoPhoneLoginViewController *vc = [[XMNoPhoneLoginViewController alloc] initWithNibName:kStringFromClass(XMNoPhoneLoginViewController) bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            XMBindPhoneLoginViewController *vc = [[XMBindPhoneLoginViewController alloc] initWithNibName:kStringFromClass(XMBindPhoneLoginViewController) bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
        }
    };
}

#pragma mark - WXManagerDelegate
// 登录回调
- (void)weChatLoginSuccessAuthResp:(SendAuthResp *)resp {
    NSLog(@"用户信息=====%@",resp);
    if (![XMUtil isBlankString:resp.code]) {
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        NSMutableDictionary *dic = @{
            @"token": resp.code
        }.mutableCopy;
        
        [YHNetworkManager.sharedInstance postWithUrl:@"api/user/wechat/update" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showHUDMsg:@"绑定成功" toView:nil];
                kUserModel.bind_wechat_status = YES;
                self.bindWXTipV.descLa.text = @"已绑定";
            }
        }];
    }
}

- (void)weChatLoginCancel {
    NSLog(@"用户取消登录");
}

- (void)weChatLoginFailed:(NSError *)error {
    NSInteger code = error.code;
    NSString *message = error.userInfo[@"message"];
    NSLog(@"错误码==%zd,错误信息===%@",code,message);
}



- (IBAction)switchAct:(UISwitch *)sender {
    
}

- (IBAction)deleteAct:(UIButton *)sender {
    [GKCover coverFrom:self.view contentView:self.zhuxiaoTipV style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleCenter) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
    
}

- (XMZhuXiaoTipView *)zhuxiaoTipV {
    if (!_zhuxiaoTipV) {
        _zhuxiaoTipV = [XMZhuXiaoTipView viewFromXib];
    }
    return _zhuxiaoTipV;
}
@end
